package com.config;

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * k8s初始化

 */
@Component
public class K8sConfig {

    /**
     * 连接地址
     */
    @Value("${k8s.server}")
    private String apiHttps;

    /**
     * 安全证书相关
     */
    @Value("${k8s.certificate-authority-data}")
    private String caCert;

    @Value("${k8s.client-certificate-data}")
    private String clientCert;

    @Value("${k8s.client-key-data}")
    private String clientKey;

    /**
     *
     * k8s初始化
     * @return
     */
    @Bean
    public KubernetesClient getFabric8Connection() {
        Config config = new ConfigBuilder().withMasterUrl(apiHttps).withTrustCerts(true)
                .withCaCertData(caCert)
                .withClientCertData(clientCert)
                .withClientKeyData(clientKey)
                .build();
        KubernetesClient kubernetesClient = new DefaultKubernetesClient(config);
        return kubernetesClient;
    }
}
